<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page="page"
        :data="tableData"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
      >
        <template slot-scope="scope" slot="menu">
          <el-button
            type="text"
            icon="el-icon-download"
            size="small"
            plain
            @click="downloadFile(scope.row)"
          >下载
          </el-button>
          <el-button
            type="text"
            icon="el-icon-document-add"
            size="small"
            plain
            @click="rebuildFile(scope.row)"
          >重新生成
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>
<script>
  import {fetchList, rebuild, download} from '@/api/antborrow/stareportfile'
  import {tableOption} from '@/const/crud/antborrow/stareportfile'
  import {mapGetters} from 'vuex'

  export default {
    name: 'stareportfile',
    data() {
      return {
        queryParams: {},
        tableData: [],
        page: {
          total: 0,
          currentPage: 1,
          pageSize: 10
        },
        tableLoading: false,
        tableOption: tableOption
      }
    },
    created() {
    },
    mounted: function () {
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    components: {},
    methods: {
      downloadFile(row) {
        this.tableLoading = true
        download(row.id).then(resp => {
          this.tableLoading = false
          if (!resp) {
            return
          }
          let blob = new Blob([resp.data]);
          let url = window.URL.createObjectURL(blob);
          let link = document.createElement('a');
          link.style.display = 'none';
          link.href = url;
          let fileName = resp.headers["content-disposition"].split(";")[1].split("filename=")[1];
          link.setAttribute('download', decodeURI(fileName));
          document.body.appendChild(link);
          link.click();
          URL.revokeObjectURL(link.href)
        })
      },
      rebuildFile(row) {
        let _this = this;
        this.$confirm('确认重新生成' + row.yearMonth + '的报表吗?', '提醒', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          _this.tableLoading = true
          return rebuild(row.id)
        }).then(res => {
          _this.tableLoading = false
          if (res.data.code === 0) {
            _this.$message.success('生成成功')
            _this.getList(this.page)
          }
        })
      },
      getList(page) {
        this.tableLoading = true
        fetchList(Object.assign({
          current: page.currentPage,
          pageSize: page.pageSize
        }, this.queryParams)).then(response => {
          this.tableData = response.data.data.records
          this.page.pageSize = response.data.data.pageSize
          this.page.total = response.data.data.total
          this.tableLoading = false
        })
      },
      /**
       */
      searchChange(form) {
        this.queryParams = this.filterForm(form)
        this.getList(this.page)
      },
      /**
       */
      refreshChange() {
        this.getList(this.page)
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>
